package com.guigu.tms.security.util;


import io.jsonwebtoken.*;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;
import java.util.UUID;

@Data
@Component
@ConfigurationProperties(prefix = "jwt")
public class JwtUtils {

    private long expire;
    private String secret;
    private String header;

    // 生成jwt
    public String generateToken(Map<String,Object> map) {
        Date expireDate = new Date(System.currentTimeMillis() + 1000 * expire);
        return Jwts.builder()
                .setId(UUID.randomUUID().toString())
                .setHeaderParam("typ", "JWT")
                .setClaims(map)
                .setExpiration(expireDate)// 过期时间
                .signWith(SignatureAlgorithm.HS256, secret)
                .compact();
    }

    // 解析jwt
    public Claims getClaimByToken(String jwt) throws JwtException {
            return Jwts.parser()
                       .setSigningKey(secret)
                       .parseClaimsJws(jwt).getBody();
    }

}